package com.xiaohai.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaohai.common.R;
import com.xiaohai.projo.Employee;
import com.xiaohai.service.EmployeeService;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;


/**
 * ClassName:EmployeeController
 * Package:IntelliJ IDEA
 * Description:
 * Author 何诗海
 *
 * @Create 2024/1/14 21:21
 * @Version 1.0
 */
@Slf4j
@RestController
@RequestMapping(value = "/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee) {
        // 1 将密码进行md5加密
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        // 2 查询数据库
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername, employee.getUsername());
        Employee emp = employeeService.getOne(queryWrapper);

        // 3 判断查询是否为null 或者 数据库中的密码与登录的密码不一致
        if (ObjectUtils.isEmpty(emp) || !emp.getPassword().equals(password)) {
            return R.error("登录失败");
        }
        if (emp.getStatus().equals(0)) {
            return R.error("账号已禁用");
        }
        request.getSession().setAttribute("employee", emp.getId());
        return R.success(emp);
    }

    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request) {
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }

    @PostMapping("/save")
    public R<String> save(@CookieValue("employee") Long id, @RequestBody Employee employee) {
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        employee.setUpdateUser(id);
        employee.setUpdateTime(new Date());
        employee.setCreateUser(id);
        employee.setCreateTime(new Date());
        employee.setSex("1");
        employeeService.save(employee);
        return R.success("新增员工成功");
    }

    @GetMapping("/page")
    public R<Page<Employee>> page(String name, int page, int pageSize) {
        // 构造分页器
        Page<Employee> employeePage = new Page<>(page, pageSize);
        // 构造条件构造器
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(!ObjectUtils.isEmpty(name), Employee::getName, name);
        // 添加排序
        queryWrapper.orderByDesc(Employee::getUpdateTime);
        // 执行查询
        Page<Employee> page1 = employeeService.page(employeePage, queryWrapper);
        return R.success(page1);
    }

    @PutMapping
    public R<String> update(@CookieValue("employee") Long id, @RequestBody Employee employee) {
        employee.setUpdateUser(id);
        employee.setUpdateTime(new Date());
        employeeService.updateById(employee);
        return R.success("员工信息修改成功");
    }

    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable Long id) {
        Employee employee = employeeService.getById(id);
        if (ObjectUtils.isEmpty(employee)){
            return R.error("没有查询到该员工信息");
        }
        return R.success(employee);
    }
}
